<?php

/**
 *   员工钉钉登录
 */

namespace app\admin\controller;

use app\facade\Basic;
use think\Request;
use app\admin\service\Dingtalk as serviceDingtalk;
use think\facade\Config;

class Dingtalk extends BaseController
{

    /**
     *  获取CODE
     */
    public function login(Request $request)
    {
        $dingtalk = new serviceDingtalk();
        $dingtalk->getCode();
    }


    /**
     *  导入钉钉部门
    */
    public function import()
    {
        $dingtalk = new serviceDingtalk();
        $result = $dingtalk->departmentImport();
    }


    /**
     *  钉钉登录回到接口
     */
    public function callback(Request $request)
    {

        $param = $request->param();
        $param['ip'] = $request->ip();
        $dingtalk = new serviceDingtalk();
        $result = $dingtalk->login($param);

        $data['userInfo'] = $result['data'];
        $data['tokenKey'] = 'X-Token';
        $data['token'] = $result['data']['token'];
        $data['roleRouters'] = [];
        $data['rememberMe'] = 1;
        $data['loginInfo'] = [
            'username' => '',
            'password' => ''
        ];

        $a = json_encode($data);
        $env = env('APP_ENV') ?? 'prod';
        $domain = Config::get('domain')[$env]['erphome'];
        echo <<<HTML
   <script>
        var domain = '{$domain}';
        window.location.href = domain + '/#/login?redirect=/admin&tt=' + window.btoa(window.encodeURIComponent('{$a}'));
    </script>
HTML;
    }
}
